﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Collections.Specialized;
using System.Data;
using System.IO;

namespace ETL
{
	class Program
	{
		static void Main(string[] args)
		{

			// make sure we have all the needed directories in place
			Config.Directories.CreateSystemDirectories();




			Config.ExcelFilesCollection xlsfiles = Config.ConfigInfo.ExcelFiles;

			for (int i = 0; i < xlsfiles.Count; i++)
			{
				try
				{
					Console.ForegroundColor = ConsoleColor.White;
					Console.Write("Transforming [" + new FileInfo(xlsfiles[i].path).Name + "]");

					DataTable dt = new Tools.Excel2DataSet(xlsfiles[i].path, xlsfiles[i].sheetname).GetExcelData();

					//string file = Config.Directories.GetOutboxDir.FullName + @"\" + Guid.NewGuid().ToString() + ".txt";
					string file = Config.Directories.GetOutboxDir.FullName + @"\" + new FileInfo(xlsfiles[i].path).Name + ".txt";
					StreamWriter writer = new StreamWriter(file, false);

					foreach (DataRow row in dt.Rows)
					{
						string line = "";
						for (int c = 0; c < dt.Columns.Count; c++)
						{
							decimal num = 0;
							if (decimal.TryParse(row[c].ToString(), out num))
								line += num.ToString("N2") + "|";
							else
								line += row[c].ToString() + "|";
						}

						line = line.TrimEnd('|');
						writer.WriteLine(line);
					}

					writer.Close();
					writer.Dispose();

					Console.ForegroundColor = ConsoleColor.Green;
					Console.WriteLine("  [OK]");
				}
				catch
				{
					Console.ForegroundColor = ConsoleColor.Red;
					Console.WriteLine("  [ERROR]");
				}
			}



			try
			{
				// upload data to FTP
				ETL.Config.FTPURLCollection ftps = Config.ConfigInfo.ConfiguredFTPs;
				for (int f = 0; f < ftps.Count; f++)
				{
					FileInfo[] outboxfiles = Config.Directories.GetOutboxDir.GetFiles();

					for (int i = 0; i < outboxfiles.Length; i++)
					{
						try
						{
							Console.ForegroundColor = ConsoleColor.White;
							Console.Write("Sending [" + outboxfiles[i].Name + "] to " + ftps[f].name);

							Tools.FTP.UploadData(ftps[f].ip, ftps[f].username, ftps[f].password, outboxfiles[i].FullName);

							if (f == ftps.Count-1)	// if final FTP move the file to the Sent dir
								File.Move(outboxfiles[i].FullName, Config.Directories.GetSentDir.FullName + "\\" + outboxfiles[i].Name);

							Console.ForegroundColor = ConsoleColor.Green;
							Console.WriteLine("  [OK]");
						}
						catch (Exception ex)
						{
							Console.ForegroundColor = ConsoleColor.Red;
							Console.WriteLine("  [ERROR]");

							File.Move(outboxfiles[i].FullName, Config.Directories.GetDeliveryErrorDir.FullName + "\\" + outboxfiles[i].Name);
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw new Exception("Error while sending the files through FTP.\n\nException:\n" + ex.Message);
			}




		}
	}
}
